
package com.joysuch.wwyt.core.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.joysuch.wwyt.core.support.Context;
import org.apache.shiro.util.Assert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.HandlerInterceptor;

public class DataImportInterceptor implements HandlerInterceptor {
    private static final Logger logger = LoggerFactory.getLogger(DataImportInterceptor.class);

    /**
     * 数据导入验证companycode，防止误导入
     */
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        if (request.getRequestURI().contains("/data-import/single")) {
            // 单次导入不做拦截
            return true;
        }
        String companyCode = request.getParameter("companyCode");
        Assert.isTrue(Context.getCompanyCode().equals(companyCode),
                "companyCode#" + companyCode + "和当前登录用户的companyCode#" + Context.getCompanyCode() + "不一致，禁止导入");
        return true;
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // Context.resetCurrentUser();
    }

    // private UserService userService;
    //
    // @Autowired
    // public void setUserService(UserService userService) {
    // this.userService = userService;
    // }
}
